package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.*;
import com.xingkeduo.pojo.dto.Manager.ManagerDTO;
import com.xingkeduo.pojo.dto.Manager.ManagerPermsDTO;
import com.xingkeduo.pojo.query.CitiesQuery;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.query.PermsQuery;
import com.xingkeduo.pojo.query.PostsPermsQuery;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.tool.DateTimeUtils;
import com.xingkeduo.tool.OperaConstants;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import sun.nio.cs.ext.MacArabic;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class ManagerDomain {
    @Autowired
    private ManagerService managerService;

    @Autowired
    private DeptmentService deptmentService;

    @Autowired
    private PostsPermsService postsPermsService;

    @Autowired
    private PermsService permsService;

    @Autowired
    private PostsService postsService;

    @Autowired
    private CitiesService citiesService;

    @Autowired
    private CountryService countryService;

    private Boolean isAdmin(ManagerDTO managerDTO){
        if (managerDTO.getIsadmin() == 1){
            return true;
        }
        return false;
    }

    public ManagerTable getManagerById(Long id) throws Throwable {

        ManagerTable managerTable = managerService.selectById(id);
        if(!CommonUtil.isEmpty(managerTable.getCitiesId())){
            CitiesTable citiesTable = citiesService.selectById(managerTable.getCitiesId());
           if(!CommonUtil.isEmpty(citiesTable)){
               managerTable.setCitiesName(citiesTable.getName());
           }
        }
        managerTable.setCreateTimes(DateTimeUtils.yyyy_MM_dd_HH_mmToDate(managerTable.getCreatedTime()));
        return managerTable;
    }

    public List<ManagerTable> getManagerList(ManagerQuery managerQuery) throws Throwable {

        List<ManagerTable> managerList = managerService.selectLists(managerQuery);
        if(CommonUtil.isEmpty(managerList)){
            return new ArrayList<>();
        }
        return managerList;
    }




    //判断管理员是否有权限
    public Boolean getManagerIsPerms(ManagerPermsDTO managerPermsDTO) throws Throwable {
        ManagerDTO managerDTO = new ManagerDTO();
        Long managerId = managerPermsDTO.getManagerId();
        String permsValue = managerPermsDTO.getPermsValue();

        ManagerTable managerTable = managerService.selectById(managerId);
        if(!CommonUtil.isEmpty(managerTable)){
            //超级管理员
            if(this.isAdmin(managerDTO)){
                return true;
            }
            List<PostsPermsTable> postsPermsList = new ArrayList<>();
            if (managerTable.getPostsId()>0L){
                //查询权限列表
                postsPermsList= postsPermsService.selectLists(new PostsPermsQuery().setPostsId(managerTable.getPostsId()));
                //所有权限的ids
                List<String> permsIdList = new ArrayList<>();
                if(!CommonUtil.isEmpty(postsPermsList) && !CommonUtil.isEmpty(postsPermsList.get(0))){
                    PostsPermsTable postsPermsTable = postsPermsList.get(0);
                    permsIdList =  CollectionUtils.arrayToList(postsPermsTable.getPermsIds().split(","));
                }

                //查询当前权限id
                PermsTable permsTable = new PermsTable();
                List<PermsTable> permsList = permsService.selectLists(new PermsQuery().setValue(permsValue));
                if (!CommonUtil.isEmpty(permsList)){
                    permsTable= permsList.get(0);
                }
                if(permsIdList.contains(permsTable.getId())){
                    return true;
                }
            }
        }
        throw new BusinessException(BusinessException.Type.DEFAULT, "no right");
    }


    //分页查询
    public Page<ManagerTable> getManagerPageList(ManagerQuery managerQuery) throws Throwable {
        Integer pageNo = managerQuery.getPageNo();
        Integer pageSize = managerQuery.getPageSize();
        Page<ManagerTable> managerTablePage = managerService.selectPageLists(managerQuery,pageNo,pageSize);
        return managerTablePage;
    }

    public int createManager(ManagerTable managerTable) throws Throwable {

        if(!CommonUtil.isEmpty(managerTable.getPhone())){
            List<ManagerTable> phone= managerService.selectLists(new ManagerQuery().setPhone(managerTable.getPhone()));
            if(!CommonUtil.isEmpty(phone)){
                throw new DataException(DataException.Type.DEFAULT, "手机号已经存在了哦!请更换手机号重试！");
            }
        }

        if (StringUtils.isNotEmpty(managerTable.getPassword())) {
            String pwd = DigestUtils.md5Hex(managerTable.getPassword() + Base64.encodeBase64String(OperaConstants.SALT.getBytes()));
            managerTable.setPassword(pwd);
        }

        DeptmentTable deptmentTable = new DeptmentTable();
        if(!CommonUtil.isEmpty(managerTable.getDeptmentId())){
            deptmentTable = deptmentService.selectById(managerTable.getDeptmentId());
        }
        if(!CommonUtil.isEmpty(deptmentTable)){
            managerTable.setDeptmentName(deptmentTable.getName());
            managerTable.setDeptmentId(deptmentTable.getId());
        }

        PostsTable postsTable = new PostsTable();
        if(!CommonUtil.isEmpty(managerTable.getPostsId())){
            postsTable = postsService.selectById(managerTable.getPostsId());
        }
        if(!CommonUtil.isEmpty(postsTable)){
            managerTable.setPostsName(postsTable.getName());
            managerTable.setPostsId(postsTable.getId());
        }

        managerTable.setSalt("xingkeduo");
        return managerService.insert(managerTable);
    }

    public int modifyManager(ManagerTable managerTable) throws Throwable {
        ManagerTable managerTable1 = managerService.selectById(managerTable.getId());
        if(!CommonUtil.isEmpty(managerTable.getStatus())){

            if(managerTable.getStatus() == 1){

                if(!CommonUtil.isEmpty(managerTable1.getPostsId())){
                    PostsTable postsTable = postsService.selectById(managerTable1.getPostsId());
                    if(postsTable.getStatus() == 2){
                        throw new DataException(DataException.Type.DEFAULT, "该账号所对应的岗位处于禁用状态，无法启用账号!");
                    }
                }
            }
        }
        if(!CommonUtil.isEmpty(managerTable.getPhone())){
            if(!managerTable.getPhone().equals(managerTable1.getPhone())){
//                if(!CommonUtil.isEmpty(managerTable.getPhone())){
                    List<ManagerTable> phone= managerService.selectLists(new ManagerQuery().setPhone(managerTable.getPhone()));
                    if(!CommonUtil.isEmpty(phone)){
                        throw new DataException(DataException.Type.DEFAULT, "手机号已存在了哦!请更换手机号重试！");
                    }
//                }
            }
        }


            if (StringUtils.isNotEmpty(managerTable.getPassword())) {
            String pwd = DigestUtils.md5Hex(managerTable.getPassword() + Base64.encodeBase64String(OperaConstants.SALT.getBytes()));
            managerTable.setPassword(pwd);
        }

        DeptmentTable deptmentTable = new DeptmentTable();
        if(!CommonUtil.isEmpty(managerTable.getDeptmentId())){
            deptmentTable = deptmentService.selectById(managerTable.getDeptmentId());
        }
        if(!CommonUtil.isEmpty(deptmentTable)){
            managerTable.setDeptmentName(deptmentTable.getName());
            managerTable.setDeptmentId(deptmentTable.getId());
        }

        PostsTable postsTable = new PostsTable();
        if(!CommonUtil.isEmpty(managerTable.getPostsId())){
            postsTable = postsService.selectById(managerTable.getPostsId());
        }
        if(!CommonUtil.isEmpty(postsTable)){
            managerTable.setPostsName(postsTable.getName());
            managerTable.setPostsId(postsTable.getId());
        }

        return managerService.update(managerTable);
    }

    //删除
    public int delManager(Long id) throws Throwable {
        ManagerTable managerTable = new ManagerTable();

        managerTable.setId(id);
        managerTable.setIsDelete(YesNoEnum.YES);
        int delRes = managerService.delete(managerTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

    public ManagerTable getLongin(ManagerTable managerTable) throws Throwable {

        if(!CommonUtil.isEmpty(managerTable.getPhone()) && !CommonUtil.isEmpty(managerTable.getPassword())){
            String phone =  managerTable.getPhone();
            String password = DigestUtils.md5Hex(managerTable.getPassword() + Base64.encodeBase64String(OperaConstants.SALT.getBytes()));
            List<ManagerTable> managerList = new ArrayList<>();
            managerList = managerService.selectLists(new ManagerQuery().setPhone(managerTable.getPhone()));

            if(!CommonUtil.isEmpty(managerList)){
                ManagerTable managerTable1 =  managerList.get(0);
                if(managerTable1.getStatus() == 2){
                    throw new DataException(DataException.Type.DEFAULT, "你的账号已被禁用！请联系管理员!");
                }else{
                    if(managerTable1.getPassword().equals(password) && managerTable1.getPhone().equals(phone)){
                        return  managerTable1;
                    }else{

                        throw new DataException(DataException.Type.DEFAULT, "密码错误!");
                    }
                }
            }else{
                throw new DataException(DataException.Type.DEFAULT, "手机号不存在!");
            }
        }else{
            throw new DataException(DataException.Type.DEFAULT, "手机号或者密码不能为空!");
        }
    }
}
